<?php

class mod
{
    //连接数据库
    public function link(){
        $mysql = new mysqli();
        $mysql->connect('127.0.0.1','root','root','goods');
        $mysql->connect_error?die('连接出错'):'连接成功';
        return $mysql;
    }

    //查
    public function select($mysql,$sql){
       $action = $mysql->query($sql);
       $concent = [];
       while (true){
           $result = $action->fetch_assoc();
           if (empty($result)){
               break;
           }
           $concent[] = $result;
       }
       return $concent;
    }

    //增删改
    public function IDU($mysql,$sql){
        $action = $mysql->query($sql);
        if ($action){
            if ($mysql->affected_rows > 0){
                return [
                    'flag' => true,
                    'msg' => '',
                ];
            }else{
                return [
                    'flag' => false,
                    'msg' => '无影响记录',
                ];
            }
        }else{
            return [
                'flag'=>false,
                'msg' =>$mysql->errno . ' ' . $mysql->error,
            ];
        }
    }

    //上传图片
    public function UP(){
        $name = $_FILES['file']['name'];
        $old_add = $_FILES['file']['tmp_name'];
        $new_add = __DIR__.'/img/'.rand(0,9999).$name;
        $move = move_uploaded_file($old_add,$new_add);
        if ($move){
            echo '图片上传成功';
        }else{
            echo '图片上传失败';
        }
        return $name;
    }

    //关闭数据库连接
    public function close($mysql){
        $mysql->close();
    }
}